package com.wanczy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanczy.pojo.RoleTable;
import com.wanczy.service.RoleTableService;
import com.wanczy.service.impl.RoleTableServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/FindByUserIdServlet")
public class FindByUserIdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		int userId = Integer.parseInt(request.getParameter("userId"));
		RoleTableService service = new RoleTableServiceImpl();
		List<RoleTable> list = service.findByUserId(userId);
		JSONArray jsonArray = new JSONArray();
		Iterator<RoleTable> it = list.iterator();
		while(it.hasNext()) {
			RoleTable role = it.next();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("roleId", role.getRole_id());
			jsonObject.put("roleName", role.getRole_name());
			jsonObject.put("roleDescribe", role.getRole_describe());
			jsonObject.put("roleState", role.getRole_state());
			jsonArray.add(jsonObject);
		}
		out.println(jsonArray.toString());
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
